package com.zhy.sunmi.fragment.setting;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.zhy.sunmi.MyApplication;
import com.zhy.sunmi.R;

import org.java_websocket.client.WebSocketClient;


public class SubSettingFragment extends Fragment {
//    private String url = MyApplication.getInstance().getUrl();
    private WebSocketClient webSocketClient;
    private View rootView;
    private EditText et_url;
    private EditText et_apikey;
    private EditText et_terminal;
    private EditText et_houseID;
    private EditText et_installerID;
    private EditText et_blankInstallerID;
    private TextView tv_submit;
    private TextView tv_wssurl;
    private static String TAG = "Socket";

    private String wws_url = "wss://sy876r670000.test.connect.paymentsense.cloud/pac?token=12164861-bcae-4ce4-9ac4-338b9048f26c&api-version=v1&software-house-id=spxus&installer-id=001";

    private String url = "wss://sy876r670000.test.connect.paymentsense.cloud";
    private String integration_type = "pac";
    private String api_key = "12164861-bcae-4ce4-9ac4-338b9048f26c";
    private String api_version = "v1";
    private String software_house_id = "sy876r67";
    private String installer_id = "sy876r67";

    private String tid = "VCMSPXUSIP0";


    public SubSettingFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_sub_setting, container, false);
            initView(rootView);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }


        return rootView;

    }

    private void initView(View view) {

        if(MyApplication.postion.equals("店长")){
            view.findViewById(R.id.cl).setVisibility(View.VISIBLE);
        }else {
            view.findViewById(R.id.cl).setVisibility(View.INVISIBLE);
        }

        view.findViewById(R.id.lay_out).setOnClickListener(v -> {
            getActivity().finish();
        });

        String M_url = url + "/" + integration_type + "?token=" + api_key + "&api-version=" + api_version + "&software-house-id=" + software_house_id + "&installer-id=" + installer_id;

        et_apikey = view.findViewById(R.id.et_apikey);
        et_blankInstallerID = view.findViewById(R.id.et_blankInstallerID);
        et_houseID = view.findViewById(R.id.et_houseID);
        et_terminal = view.findViewById(R.id.et_terminal);
        et_url = view.findViewById(R.id.et_url);
        et_installerID = view.findViewById(R.id.et_installerID);
        tv_submit = view.findViewById(R.id.tv_submit);
        tv_wssurl = view.findViewById(R.id.textView20);


        et_apikey.setText(api_key);
        et_url.setText(url);
        et_houseID.setText(software_house_id);
        et_installerID.setText(installer_id);
        et_blankInstallerID.setText(installer_id);
        et_terminal.setText(tid);

        tv_wssurl.setText(MyApplication.getInstance().getUrl());

        tv_submit.setOnClickListener(v -> {
            String M_murl = et_url.getText().toString().trim() + "/" + integration_type + "?token=" + et_apikey.getText().toString().trim() + "&api-version=" + api_version + "&software-house-id=" + software_house_id + "&installer-id=" + et_installerID.getText().toString().trim();
            tv_wssurl.setText(M_murl);
            MyApplication.getInstance().saveUrl(M_murl);
            MyApplication.getInstance().saveTid(et_terminal.getText().toString().trim());
            Toast.makeText(getContext(),"Success",Toast.LENGTH_SHORT).show();
            getActivity().finish();

        });


        MyApplication.getInstance().setHandler(handler);



        view.findViewById(R.id.tv_report).setOnClickListener(v -> {

            if(MyApplication.getInstance().getWebSocketClient().isOpen()){
                String tid=MyApplication.getInstance().getTid();
                String str="{" + "  \"jsonrpc\": \"2.0\"," + "  \"method\": \"reportEndOfDay\"," + "  \"params\": { \"tid\": \""+tid+"\" }," + "  \"id\": 8" + "}";
//                String str = "{ \"jsonrpc\": \"2.0\", \"method\": \"sale\", \"params\": { \"tid\": \""+tid+"\", \"currency\": \"GBP\", \"amount\":"+aa+" },\"id\":"+id+" }";
                MyApplication.getInstance().getWebSocketClient().send(str);

            }


        });





    }


    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            Gson gson = new Gson();
            Log.d("Socket", msg.obj.toString());
            return false;
        }
    });
}